Added `-[NSArray validateAsPropertyList]` and `-[NSDictionary validateAsPropertyList...
[adiumx.git] / Frameworks / AIUtilities Framework / Source / AIArrayAdditions.m
blobbd266d03e7a93dd1170f66a974ca4198aa5c93af
1 //
2 //  AIArrayAdditions.m
3 //  AIUtilities.framework
4 //
5 //  Created by Evan Schoenberg on 2/15/05.
6 //  Copyright 2005 The Adium Team. All rights reserved.
7 //
9 #import "AIArrayAdditions.h"
11 @implementation NSArray (AIArrayAdditions)
13 - (BOOL)containsObjectIdenticalTo:(id)obj
15         return ([self indexOfObjectIdenticalTo:obj] != NSNotFound);
18 // Returns an array from the owners bundle with the specified name
19 + (NSArray *)arrayNamed:(NSString *)name forClass:(Class)inClass
21     NSBundle            *ownerBundle;
22     NSString            *arrayPath;
23     
24     //Get the bundle
25     ownerBundle = [NSBundle bundleForClass:inClass];
26     
27     //Open the plist file
28     arrayPath = [ownerBundle pathForResource:name ofType:@"plist"];    
30     return [[[NSArray alloc] initWithContentsOfFile:arrayPath] autorelease];
33 - (NSComparisonResult)compare:(NSArray *)other
35         NSComparisonResult result = NSOrderedSame;
37         NSEnumerator *selfEnum = [self objectEnumerator], *otherEnum = [other objectEnumerator];
38         id selfObj, otherObj = nil;
39         while ((result == NSOrderedSame) && (selfObj = [selfEnum nextObject]) && (otherObj = [otherEnum nextObject])) {
40                 result = [selfObj compare:otherObj];
41         }
43         if (result == NSOrderedSame) {
44                 if (selfObj && !otherObj) {
45                         result = NSOrderedDescending;
46                 } else if(otherObj && !selfObj) {
47                         result = NSOrderedAscending;
48                 }
49         }
51         return result;
54 - (BOOL)validateAsPropertyList
56         BOOL validated = YES;
57         NSEnumerator *enumerator = [self objectEnumerator];
58         id      value;
60         while ((value = [enumerator nextObject])) {
61                 Class valueClass = [value class];
62                 if (![value isKindOfClass:[NSString class]] &&
63                         ![value isKindOfClass:[NSData class]] &&
64                         ![value isKindOfClass:[NSNumber class]] &&
65                         ![value isKindOfClass:[NSArray class]] &&
66                         ![value isKindOfClass:[NSDictionary class]] &&
67                         ![value isKindOfClass:[NSDate class]]) {
68                         NSLog(@"** Array failed validation: %@: Value %@ is a %@ but must be a string, data, number, array, dictionary, or date",
69                                   self, value, NSStringFromClass(valueClass));
70                         validated = NO;
71                 }
73                 if ([value isKindOfClass:[NSArray class]] ||[value isKindOfClass:[NSDictionary class]]) {
74                         BOOL successOfValue = [value validateAsPropertyList];
75                         if (validated) validated = successOfValue;
76                 }
77         }
78         
79         return validated;
82 @end
84 @implementation NSMutableArray (ESArrayAdditions)
86 - (void)addObjectsFromArrayIgnoringDuplicates:(NSArray *)inArray
88         NSEnumerator    *enumerator = [inArray objectEnumerator];
89         id                              object;
90         
91         while ((object = [enumerator nextObject])) {
92                 if (![self containsObject:object]) [self addObject:object];
93         }
96 - (void)moveObject:(id)object toIndex:(unsigned)newIndex
98         unsigned        currentIndex = [self indexOfObject:object];
99         NSAssert3(currentIndex != NSNotFound, @"%@ %p does not contain object %p", NSStringFromClass([self class]), self, object);
100         
101         //if we're already there, do no work
102         if (currentIndex == newIndex) return;
103         
104         //Account for shifting
105         if (currentIndex <  newIndex) newIndex--;
106         
107         //Move via a remove and add :(
108         [object retain];
109         [self removeObject:object];
110         [self insertObject:object atIndex:newIndex];
111         [object release];
114 //just a better name for an existing NSMutableArray method.
115 //this makes it uniform in style with -[NSMutableDictionary setObject:forKey:].
116 - (void)setObject:(id)object atIndex:(unsigned)index
118         [self replaceObjectAtIndex:index withObject:object];
121 @end